package com.ipzoe.common.captcha;

import com.ipzoe.common.captcha.config.CaptchaCodeProperties;
import lombok.Setter;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * 默认验证码生成器
 * 
 * @author Eric
 * @date 2019/12/02
 */
public class DefaultCaptchaCodeGenerator implements CaptchaCodeGenerator {

    @Setter
    private CaptchaCodeProperties captchaCodeProperties;

    public DefaultCaptchaCodeGenerator(CaptchaCodeProperties captchaCodeProperties) {
        this.captchaCodeProperties = captchaCodeProperties;
    }

    public DefaultCaptchaCodeGenerator() {

    }

    @Override
    public CaptchaCode generate() {
        String code = RandomStringUtils.random(captchaCodeProperties.getLength(), captchaCodeProperties.getContainLetter(),
                captchaCodeProperties.getContainNumber());
        return new CaptchaCode(captchaCodeProperties.getExpireIn(), code);
    }

    public CaptchaCodeProperties getCodeProperties() {
        return captchaCodeProperties;
    }

    public void setCodeProperties(CaptchaCodeProperties captchaCodeProperties) {
        this.captchaCodeProperties = captchaCodeProperties;
    }

}
